package com.hong.spring.grpc;

import com.alibaba.fastjson.JSONObject;
import com.hong.spring.Greeting;
import com.hong.spring.HelloResp;
import com.hong.spring.HelloWorldGrpc;
import com.hong.spring.api.IUserService;
import com.hong.spring.common.SpringContextUtils;
import com.hong.spring.entity.User;
import com.hong.spring.utils.DataResponse;
import io.grpc.stub.StreamObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * 功能描述: 响应接口
 *
 * @param:
 * @return:
 * @auther: csh
 * @date: 2021/1/27 11:45
 */
@Service
public class HelloWorldRpcService extends HelloWorldGrpc.HelloWorldImplBase {

    @Autowired
    private IUserService userService;

    @Override
    public void sayHello(Greeting request, StreamObserver<HelloResp> responseObserver) {
        IUserService bean = SpringContextUtils.getBean(IUserService.class);
        DataResponse <List <User>> byAll = bean.findByAll();
        HelloResp resp = HelloResp.newBuilder()
                .setReply(JSONObject.toJSONString(byAll))
                .build();
        responseObserver.onNext(resp);
        responseObserver.onCompleted();
    }
}